package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import POJO.TCC;

public class TCCDao 
{
	Connection c;
	
	public TCCDao()
	{
		GenericDao gDao = new GenericDao();
		c = gDao.getConnection();
	}
	
	public void CadastrarTCC(TCC tcc) throws SQLException
	{
		String sql = "INSERT INTO TCC (tema, codInstituicao, autor, semestre, ano, codCurso)"
				+ " VALUES (?, ?, ?, ?, ?, ?)";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setString(1, tcc.getTema());
		ps.setInt(2, tcc.getCodInstituicao());
		ps.setString(3, tcc.getAutor());
		ps.setString(4, tcc.getSemestre());
		ps.setInt(5, tcc.getAno());
		ps.setInt(6, tcc.getCodCurso());
		ps.execute();
		ps.close();
	}
	
	public void AlterarTCC(TCC tcc) throws SQLException
	{
		String sql = "UPDATE TCC SET tema=?, codInstituicao=?, autor=?, semestre=?, ano=?, codCurso=?"
				+ "WHERE codTCC=?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setString(1, tcc.getTema());
		ps.setInt(2, tcc.getCodInstituicao());
		ps.setString(3, tcc.getAutor());
		ps.setString(4, tcc.getSemestre());
		ps.setInt(5, tcc.getAno());
		ps.setInt(6, tcc.getCodCurso());
		ps.setInt(7, tcc.getCodTCC());
		ps.execute();
		ps.close();
	}
	
	public void DeletaTCC(TCC tcc) throws SQLException
	{
		String sql = "DELETE TCC WHERE codTCC = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, tcc.getCodTCC());
		ps.execute();
		ps.close();
	}
	
	public int ProximoCodigo() throws SQLException
	{
		int codigo=1;
		TCC tcc = new TCC();
		String sql = "SELECT MAX(codTCC + 1) AS codTCC FROM TCC";
		PreparedStatement ps = c.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		if(rs.next())
		{
			codigo = rs.getInt("codTCC");
		}
		rs.close();
		ps.close();
		return codigo;
	}
	
	public TCC ConsultaTCC(TCC tcc) throws SQLException
	{
		String sql = "SELECT codTCC, tema, codInstituicao, autor, semestre, ano, codCurso FROM TCC WHERE codTCC=? ";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, tcc.getCodTCC());
		ResultSet rs = ps.executeQuery();
		if(rs.next())
		{
			tcc.setCodTCC(rs.getInt("codTCC"));
			tcc.setTema(rs.getString("tema"));
			tcc.setCodInstituicao(rs.getInt("codInstituicao"));
			tcc.setAutor(rs.getString("autor"));
			tcc.setSemestre(rs.getString("semestre"));
			tcc.setAno(rs.getInt("ano"));
			tcc.setCodCurso(rs.getInt("codCurso"));
		}
		rs.close();
		ps.close();
		return tcc;
	}
	
	public List<TCC> listaTCC() throws SQLException
	{
		List<TCC> listaTCC = new ArrayList<TCC>();
		String sql = "SELECT * FROM TCC ORDER BY tema";
		PreparedStatement ps = c.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		while(rs.next())
		{
			TCC tcc = new TCC();
			tcc.setCodTCC(rs.getInt("codTCC"));
			tcc.setTema(rs.getString("tema"));
			tcc.setCodInstituicao(rs.getInt("codInstituicao"));
			tcc.setAutor(rs.getString("autor"));
			tcc.setSemestre(rs.getString("semestre"));
			tcc.setAno(rs.getInt("ano"));
			tcc.setCodCurso(rs.getInt("codCurso"));
			listaTCC.add(tcc);
		}
		rs.close();
		ps.close();
		return listaTCC;
	}
}